package cn.tworice.client.service.impl;

import cn.tworice.client.dao.TopicDao;
import cn.tworice.client.service.TopicService;
import cn.tworice.common.entity.Answer;
import cn.tworice.common.entity.Topic;
import cn.tworice.common.entity.dto.TopicDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    private TopicDao topicDao;

    @Override
    public Integer getTotal() {
        return topicDao.queryTotal();
    }

    @Override
    public List<TopicDto> getList(Integer page, Integer pageSize) {
        return topicDao.queryList(page * pageSize, pageSize);
    }

    @Override
    public String setTopic(Topic topic) {
        topic.setDate(System.currentTimeMillis());
        if (topic.getId() == null || topic.getId().isEmpty()) {
            topic.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            topicDao.insertTopic(topic);
        }else{
            topicDao.updateTopic(topic);
        }
        return topic.getId();
    }

    @Override
    public Integer del(String[] id) {
        topicDao.delAnswerByTopicId(id);
        return topicDao.delByIds(id);
    }

    @Override
    public void setAnswer(List<Answer> answerList, String id) {
        String[] ids = {id};
        topicDao.delAnswerByTopicId(ids);
        for(Answer answer:answerList){
            answer.setTopicId(id);
            answer.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        }
        topicDao.insertAnswer(answerList);
    }

    @Override
    public List<Answer> getAnswerByTopicId(String topicId) {
        return topicDao.queryAnswerByTopicId(topicId);
    }

    @Override
    public List<TopicDto> getTopicListByBankId(String bankId) {
        return topicDao.queryTopicByBankId(bankId);
    }
}
